package com.haidechizi.fastadmin.modules.code.controller;

import com.alibaba.fastjson.JSON;
import com.haidechizi.fastadmin.common.utils.Response;
import com.haidechizi.fastadmin.common.validator.ValidatorUtils;
import com.haidechizi.fastadmin.common.validator.group.AddGroup;
import com.haidechizi.fastadmin.common.validator.group.UpdateGroup;
import com.haidechizi.fastadmin.modules.code.entity.DynamicCodeConfig;
import com.haidechizi.fastadmin.modules.code.entity.MessageCodeConfig;
import com.haidechizi.fastadmin.modules.code.entity.PropCodeConfig;
import com.haidechizi.i18n.code.generator.common.dynamic.generator.DynamicCodeGenerator;
import com.haidechizi.i18n.code.generator.common.dynamic.param.DynamicI18nConfig;
import com.haidechizi.i18n.code.generator.common.message.generator.Generators;
import com.haidechizi.i18n.code.generator.common.message.param.Param;
import com.haidechizi.i18n.code.generator.common.propconvert.generator.PropGenerators;
import com.haidechizi.i18n.code.generator.common.propconvert.param.PropConvertConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/code/generator")
public class GeneratorController {

    @Autowired
    private Generators generators;

    @Autowired
    private DynamicCodeGenerator dynamicCodeGenerator;

    @Autowired
    private PropGenerators propGenerators;

    @PostMapping("message")
    public Response generator(@RequestBody MessageCodeConfig messageCodeConfig) {
        ValidatorUtils.validateEntity(messageCodeConfig);
        Param param = new Param();
        BeanUtils.copyProperties(messageCodeConfig, param);
        return Response.ok().put("data", generators.generator(param));
    }


    @PostMapping("dynamic/init")
    public Response init(@RequestBody DynamicCodeConfig dynamicCodeConfig) {
        ValidatorUtils.validateEntity(dynamicCodeConfig, AddGroup.class);
        DynamicI18nConfig i18nConfig = new DynamicI18nConfig();
        BeanUtils.copyProperties(dynamicCodeConfig, i18nConfig);
        return Response.ok().put("data", dynamicCodeGenerator.getI18nField(i18nConfig));
    }


    @PostMapping("dynamic/generator")
    public Response codeGenerator(@RequestBody DynamicCodeConfig dynamicCodeConfig) {
        ValidatorUtils.validateEntity(dynamicCodeConfig, UpdateGroup.class);
        DynamicI18nConfig i18nConfig = JSON.parseObject(JSON.toJSONString(dynamicCodeConfig), DynamicI18nConfig.class);
        dynamicCodeGenerator.generatorFile(i18nConfig);
        return Response.ok();
    }

    @PostMapping("languageDataGenerator")
    public Response languageDataGenerator(@RequestBody PropCodeConfig propCodeConfig) {
        ValidatorUtils.validateEntity(propCodeConfig);
        PropConvertConfig propConvertConfig = new PropConvertConfig();
        BeanUtils.copyProperties(propCodeConfig, propConvertConfig);
        propGenerators.generator(propConvertConfig);
        return Response.ok();
    }

}
